//
//  DrawerViewController.m
//  NewMusic
//
//  Created by dllo on 15/11/11.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "DrawerViewController.h"
#import "LabelTableViewCell.h"
#import "HeadView.h"
#import "DrawFootView.h"
#import "ButtonTableViewCell.h"
#import "HQTimeView.h"
#import "DataShare.h"
@interface DrawerViewController ()<UITableViewDelegate, UITableViewDataSource, HQgetMusic, HQRemoveTimeView>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSArray *dataArray;//功能数组
@property (nonatomic, retain) NSArray *picArray;//图片数组
@property (nonatomic, assign) NSInteger selectedNum;//tableView点击纪录
@property (nonatomic, retain) HQTimeView *timeView;//自定义图片
@property (nonatomic, retain) NSTimer *timer;//数字定时器
@property (nonatomic, assign) NSInteger timeNum;//记录数组
@end

@implementation DrawerViewController
- (void)dealloc {
    [_tableView release];
    [_dataArray release];
    [_picArray release];
    [_timeView release];
    [_timer release];
    [super dealloc];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 添加清扫时间
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [self.view addGestureRecognizer:pan];
    [pan release];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:@"backGroud" ofType:@"jpg"]]];
    // 创建数据源 && 尾视图
    [self creatDataAndHeadVew];
    // 创建tableView
    [self creatTableView];
    // 创建尾视图
    [self creatFootView];
}
// 创建顶部视图
- (void)creatDataAndHeadVew
{
    HeadView *headView = [[HeadView alloc] initWithFrame:CGRectMake(0, 0, 375, 80)];
    [self.view addSubview:headView];
    [headView release];
    self.dataArray = [NSArray arrayWithObjects:@"音质选择", @"定时关闭", @"锁屏播放",  @"省电模式", @"清理缓存", @"关于我们", nil];
    self.picArray = [NSArray arrayWithObjects:@"draw-yinzhi", @"draw-naoling", @"draw-suo", @"draw-dianchi", @"draw-qinglihuancun", @"draw-me", nil];
}
// 退出登录视图
- (void)creatFootView
{
    DrawFootView *footView = [[DrawFootView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)];
    self.tableView.tableFooterView = footView;
    [footView release];
}
// 创建tableView
- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 80, 300, 547) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.scrollEnabled = NO;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[LabelTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerClass:[ButtonTableViewCell class] forCellReuseIdentifier:@"cellB"];
    [_tableView release];
    [self.view addSubview:self.timeView];
}
// 清扫收回
- (void)panAction:(UIPanGestureRecognizer *)sender
{
    [self.delegate changeWindowWithDrawerViewController];
}
// 第一个cell点击刷新
- (void)TapchangeSelected:(NSInteger)num
{
    if (0 != num)
    {
        self.selectedNum = num;
        [self.tableView beginUpdates];
        NSIndexPath *index = [NSIndexPath indexPathForRow:num inSection:0];
        [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:index, nil] withRowAnimation:UITableViewRowAnimationTop];
        [self.tableView endUpdates];
        
    } else {
        [self.tableView beginUpdates];
        NSIndexPath *index = [NSIndexPath indexPathForRow:self.selectedNum inSection:0];
        self.selectedNum = 0;
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:index, nil] withRowAnimation:UITableViewRowAnimationTop];
        [self.tableView endUpdates];
    }
}
// 定时器触发
- (void)BtnChangeSelected:(NSInteger)num
{
    if (0 != num)
    {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 667-260)];
        view.tag = 11010;
        view.backgroundColor = [UIColor clearColor];
        [self.view addSubview:view];
        [view release];
        [UIView animateWithDuration:.5f animations:^{
            self.timeView.frame = CGRectMake(0, 667-260, 300, 260);
            view.alpha = 0.6;
            view.backgroundColor = [UIColor blackColor];
        }];
    } else {
        [UIView animateWithDuration:.5f animations:^{
            self.timeView.frame = CGRectMake(0, 667, 300, 260);
        }];
        [self.timer invalidate];
        self.timer = nil;
    }
}
// 移除timeView视图
- (void)removeTimeView:(NSInteger)num
{
    UIView *view = (UIView *)[self.view viewWithTag:11010];
    [view removeFromSuperview];
    if (self.timeNum != 0)
    {
        [self.timer invalidate];
        self.timer = nil;
        [[DataShare shareHandle] removeTime];
    }
    [UIView animateWithDuration:.5f animations:^{
        self.timeView.frame = CGRectMake(0, 667, 300, 260);
    }];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeTime) userInfo:nil repeats:YES];
    self.timeNum = num * 60;
    [[DataShare shareHandle] HaveTimer:self.timeNum];
}
// 改变cell的的数字
- (void)changeTime
{
    NSIndexPath *index = nil;
    if (0 == self.selectedNum)
    {
        index = [NSIndexPath indexPathForRow:1 inSection:0];
    } else {
        index = [NSIndexPath indexPathForRow:2 inSection:0];
    }
    LabelTableViewCell *cell = [self.tableView cellForRowAtIndexPath:index];
    [cell changeLabelNumWith:self.timeNum];
    self.timeNum--;
}
// 懒加载时间选择器
- (HQTimeView *)timeView
{
    if (nil == _timeView)
    {
        self.timeView = [[HQTimeView alloc] initWithFrame:CGRectMake(0, 667, 300, 260)];
        self.timeView.delegate = self;
        [_timeView release];
    }
    return _timeView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (0 == section)
    {
        if (self.selectedNum != 0)
        {
            return self.dataArray.count;
        }
        return self.dataArray.count - 1;
    }
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
// 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.row && 1 == indexPath.section)//关于我们点击
    {
        [self.delegate changeWindowWithDrawerViewController];
        [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(change) userInfo:nil repeats:NO];
    }
    if (0 == self.selectedNum)
    {
        if (4 == indexPath.row)
        {
            LabelTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
            [cell labelToZero];
        }
    } else {
        if (5 == indexPath.row)
        {
            LabelTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
            [cell labelToZero];
        }
    }
    
}
// 关于我们消息发送
- (void)change
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushTocallMe" object:self userInfo:nil];
}
// 关联cell(关键)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == self.selectedNum)
    {
        LabelTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
        if (0 == indexPath.section)
        {
            cell.num = indexPath.row;
            cell.delegate = self;
            cell.labelName = self.dataArray[indexPath.row];
            cell.picName = self.picArray[indexPath.row];
        } else {
            cell.num = 5;
            cell.labelName = self.dataArray[5];
            cell.picName = self.picArray[5];
        }
        return cell;
    } else {
        if (0 == indexPath.section)
        {
            if (0 == indexPath.row)
            {
                LabelTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.backgroundColor = [UIColor clearColor];
                cell.num = indexPath.row;
                cell.delegate = self;
                cell.labelName = self.dataArray[indexPath.row];
                cell.picName = self.picArray[indexPath.row];
                return cell;
            } else if (1 == indexPath.row) {
                ButtonTableViewCell *cellB = [self.tableView dequeueReusableCellWithIdentifier:@"cellB"];
                cellB.selectionStyle = UITableViewCellSelectionStyleNone;
                cellB.backgroundColor = [UIColor clearColor];
                return cellB;
            } else {
                LabelTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.backgroundColor = [UIColor clearColor];
                cell.num = indexPath.row;
                cell.delegate = self;
                cell.labelName = self.dataArray[indexPath.row + 1];
                cell.picName = self.picArray[indexPath.row + 1];
                return cell;
            }
        } else {
            LabelTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor = [UIColor clearColor];
            cell.num = 5;
            cell.labelName = self.dataArray[5];
            cell.picName = self.picArray[5];
            return cell;
        }
    }
    
}
//
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.selectedNum != 0)
    {
        if (0 == indexPath.section && self.selectedNum == indexPath.row) {
            return 40;
        }
    }
    return 60;
}
// 头视图距离
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 15;
}
// 头视图改色
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
{
    view.tintColor = [UIColor clearColor];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
